import pyvisa as visa
import time
class Demo():
     def __init__(self, ip, visaDLL=None, *args):
         self.ip = ip
         self.visaDLL = 'C:/Windows/System32/visa32.dll' 
         self.address = 'TCPIP::%s::SOCKET' % self.ip
         self.resourceManager = visa.ResourceManager(self.visaDLL)

     def open(self):
         self.instance = self.resourceManager.open_resource(self.address)     
         self.instance.read_termination = '\n'    # 定义结束符

     def close(self):
         if self.instance is not None:
             self.instance.close()
             self.instance = None

     def set(self):
         self.instance.write('*RST\n')
         self.instance.write(':SENS:BWID 1KHZ\n')
         self.instance.write(':SENS:FREQ:CENT 10000000\n')

     def S12_M(self):
     	 self.instance.write(':DISP:WIND:TRAC1:DEL\n')
     	 self.instance.write(":CALCulate1:PARameter:DEF 'CH1',S12\n")
     	 self.instance.write(":DISP:WIND:TRAC1:FEED 'CH1'\n")
     	 self.instance.write(":CALCulate1:PARameter:SEL 'CH1'\n")
     	 self.instance.write(':CALCulate1:FORMat MLOGarithmic\n')
     	 self.instance.write(':CALCulate1:FUNCtion:STAT:STAT ON\n')
     	 self.instance.write(':CALCulate1:FUNCtion:TYPE STDEV\n')
     	 time.sleep(1)
     	 pr=self.instance.query(':CALCulate1:FUNCtion:DATA?\n')
     	 print('S12 MLOGarithmic STDEV is '+pr)

     def S12_P(self):
     	 self.instance.write(":CALCulate1:PARameter:DEF 'CH2',S12\n")
     	 self.instance.write(":DISP:WIND:TRAC2:FEED 'CH2'\n")
     	 self.instance.write(":CALCulate1:PARameter:SEL 'CH2'\n")
     	 self.instance.write(':CALCulate1:FORMat PHASe\n')
     	 self.instance.write(':CALCulate1:FUNCtion:STAT:STAT ON\n')
     	 self.instance.write(':CALCulate1:FUNCtion:TYPE STDEV\n')
     	 time.sleep(1)
     	 pr=self.instance.query(':CALCulate1:FUNCtion:DATA?\n')
     	 print('S12 PHASe STDEV is '+pr)
     	
     def S21_M(self):
     	 self.instance.write(":CALCulate1:PARameter:DEF 'CH3',S21\n")
     	 self.instance.write(":DISP:WIND:TRAC3:FEED 'CH3'\n")
     	 self.instance.write(":CALCulate1:PARameter:SEL 'CH3'\n")
     	 self.instance.write(':CALCulate1:FORMat MLOGarithmic\n')
     	 self.instance.write(':CALCulate1:FUNCtion:STAT:STAT ON\n')
     	 self.instance.write(':CALCulate1:FUNCtion:TYPE STDEV\n')
     	 time.sleep(1)
     	 pr=self.instance.query(':CALCulate1:FUNCtion:DATA?\n')
     	 print('S21 MLOGarithmic STDEV is '+pr)
     	 
     def S21_P(self):
     	 self.instance.write(":CALCulate1:PARameter:DEF 'CH4',S21\n")
     	 self.instance.write(":DISP:WIND:TRAC4:FEED 'CH4'\n")
     	 self.instance.write(":CALCulate1:PARameter:SEL 'CH4'\n")
     	 self.instance.write(':CALCulate1:FORMat PHASe\n')
     	 self.instance.write(':CALCulate1:FUNCtion:STAT:STAT ON\n')
     	 self.instance.write(':CALCulate1:FUNCtion:TYPE STDEV\n')
     	 time.sleep(1)
     	 pr=self.instance.query(':CALCulate1:FUNCtion:DATA?\n')
     	 print('S21 PHASe STDEV is '+pr)




if __name__ == '__main__':
     de = Demo('127.0.0.1::5025')
     de.open()
     de.set()
     de.S12_M()
     de.S12_P()
     de.S21_M()
     de.S21_P()
     de.close()

